// 数据统计
app.controller('datastatistics', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', function ($scope, $http, commonFactory, toaster, $timeout) {

    $scope.PriceStatistic = {
        time: GetDateStr(-1),
        timet: GetDateStr(0)
    }
    function GetDateStr(AddDayCount) {
        var dd = new Date();
        dd.setDate(dd.getDate() + AddDayCount);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1;
        var d = dd.getDate();
        return y + "-" + m + "-" + d;
    }
    var PriceStatistic = function () {
        if ($scope.PriceStatistic.time || $scope.PriceStatistic.timet) {
            $scope.PriceStatistic.time = commonFactory.DateFormat($scope.PriceStatistic.time, 1)
            $scope.PriceStatistic.timet = commonFactory.DateFormat($scope.PriceStatistic.timet, 1)
        }
        if ($scope.PriceStatistic.time.indexOf('NaN') != -1) {
            $scope.PriceStatistic.time = '1990-01-01'
        }
        commonFactory.loading(true)
        commonFactory.ajax(commonFactory.baseurlstatistic + "/Other/PriceStatistic", { Dt1: $scope.PriceStatistic.time, Dt2: $scope.PriceStatistic.timet }, function (result) {
            $scope.graybtn = false;
            commonFactory.loading(false)
            if (result.Code == 1) {
                $scope.PriceStatisticList = result.PriceStatisticInfo;
            } else {
                toaster.pop('error', '失败', result.ResposeData)
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    $scope.querydata = function () {
        $scope.graybtn = true;
        PriceStatistic()
    }
    PriceStatistic()

}]);
// 渠道复投统计
app.controller('redelivery', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', function ($scope, $http, commonFactory, toaster, $timeout) {
    $scope.redeliveryData = {
        DateStart: '',
        DateEnd: ''
    }
    var getRedelivery = function () {
        if (!$scope.redeliveryData.DateStart || !$scope.redeliveryData.DateEnd) {
            toaster.pop('warning', '提示', "开始时间和结束时间必填");
            $scope.graybtn = false;
            return;
        } else {
            $scope.redeliveryData.DateStart = commonFactory.DateFormat($scope.redeliveryData.DateStart, 1)
            $scope.redeliveryData.DateEnd = commonFactory.DateFormat($scope.redeliveryData.DateEnd, 1)
        }
        commonFactory.loading(true)
        commonFactory.ajax("/api/Other/SourceReCast", $scope.redeliveryData, function (result) {
            $scope.graybtn = false;
            commonFactory.loading(false)
            if (result.Code == 1) {
                $scope.redeliveryList = JSON.parse(result.ResposeData);
            } else {
                toaster.pop('error', '失败', result.ResposeData)
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    $scope.querydata = function () {
        $scope.graybtn = true;
        getRedelivery()
    }

}]);
// 经营日报
app.controller('business', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$modal', function ($scope, $http, commonFactory, toaster, $timeout, $modal) {
    var today, yestoday, todaymonth, todayday, yesmonth, yesday, starttime, endtime;
    today = new Date(),
        yestoday = new Date(today.getTime() - 86400000),
        todaymonth = today.getMonth() + 1,
        todayday = today.getDate(),
        yesmonth = yestoday.getMonth() + 1,
        yesday = yestoday.getDate(),
        todaymonth = todaymonth > 9 ? todaymonth : ('0' + todaymonth),
        todayday = todayday > 9 ? todayday : '0' + todayday;
    yesmonth = yesmonth > 9 ? yesmonth : ('0' + yesmonth),
        yesday = yesday > 9 ? yesday : '0' + yesday;
    starttime = yestoday.getFullYear() + '-' + yesmonth + '-' + yesday;
    endtime = today.getFullYear() + '-' + todaymonth + '-' + todayday;
    $scope.businessData = {
        StartDate: starttime,
        EndDate: endtime
    }
    var getbusiness = function () {
        if (!$scope.businessData.StartDate || !$scope.businessData.EndDate) {
            toaster.pop('warning', '提示', "开始时间和结束时间必填");
            $scope.graybtn = false;
            return;
        } else {
            $scope.businessData.StartDate = commonFactory.DateFormat($scope.businessData.StartDate, 1)
            $scope.businessData.EndDate = commonFactory.DateFormat($scope.businessData.EndDate, 1)
        }
        commonFactory.loading(true)
        commonFactory.ajax("/api/Statistics/QueryBusiness", $scope.businessData, function (result) {
            $scope.graybtn = false;
            commonFactory.loading(false)
            if (result.Code == 1) {
                $scope.businessList = JSON.parse(result.Data);
            } else {
                toaster.pop('error', '失败', result.ResposeData)
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    getbusiness()
    $scope.querydata = function () {
        $scope.graybtn = true;
        getbusiness()
    }
    // 充值总额提现总额的明细
    $scope.cashDetail = function (otype, odate) {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Statistics/QueryPriceDetail", { Date: odate, Type: otype }, function (result) {
            commonFactory.loading(false)
            if (result.Code == 1) {
                var modalInstance = $modal.open({
                    templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                    controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', function ($scope, $modalInstance, toaster, commonFactory) {
                        if (otype == 2) {
                            $scope.rechageshow = true;
                        } else {
                            $scope.withdrawshow = true;
                        }
                        $scope.cashDetailList = JSON.parse(result.Data);
                        $scope.cancel = function () {
                            $modalInstance.close('cancel');
                        };
                    }],
                    size: '',
                    backdrop: true
                });

            } else {
                toaster.pop('error', '失败', result.Data);
            }
        }, function () {
            toaster.pop('error', '失败', '后台未返回报错');
        }, "post");
    }
    // 账户余额明细
    $scope.tatolDetail = function (odate) {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Statistics/QueryUsablePrice", { Date: odate }, function (result) {
            commonFactory.loading(false)
            if (result.Code == 1) {
                var modalInstance = $modal.open({
                    templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                    controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', function ($scope, $modalInstance, toaster, commonFactory) {
                        $scope.tatolDetailList = JSON.parse(result.Data);
                        $scope.cancel = function () {
                            $modalInstance.close('cancel');
                        };
                    }],
                    size: 'lg',
                    backdrop: false
                });

            } else {
                toaster.pop('error', '失败', result.Data);
                $modalInstance.close();
            }
        }, function () {
            toaster.pop('error', '失败', '后台未返回报错');
        }, "post");
    }

}]);
// 渠道统计
app.controller('channellistview', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$modal', function ($scope, $http, commonFactory, toaster, $timeout, $modal) {
    var today, yestoday, todaymonth, todayday, yesmonth, yesday, starttime, endtime, sourceTypesArr = [];
    today = new Date(),
        yestoday = new Date(today.getTime() - 86400000),
        todaymonth = today.getMonth() + 1,
        todayday = today.getDate(),
        yesmonth = yestoday.getMonth() + 1,
        yesday = yestoday.getDate(),
        todaymonth = todaymonth > 9 ? todaymonth : ('0' + todaymonth),
        todayday = todayday > 9 ? todayday : '0' + todayday;
    yesmonth = yesmonth > 9 ? yesmonth : ('0' + yesmonth),
        yesday = yesday > 9 ? yesday : '0' + yesday;
    starttime = yestoday.getFullYear() + '-' + yesmonth + '-' + yesday;
    endtime = today.getFullYear() + '-' + todaymonth + '-' + todayday;
    $scope.channellistviewData = {
        StartDate: starttime,
        EndDate: endtime,
        SourceTypeIds: ""
    }
    var getchannellist = function () {
        $scope.exportshow = false;
        if (!$scope.channellistviewData.StartDate || !$scope.channellistviewData.EndDate) {
            toaster.pop('warning', '提示', "开始时间和结束时间必填");
            $scope.graybtn = false;
            return;
        } else {
            $scope.channellistviewData.StartDate = commonFactory.DateFormat($scope.channellistviewData.StartDate, 1)
            $scope.channellistviewData.EndDate = commonFactory.DateFormat($scope.channellistviewData.EndDate, 1)
        }

        commonFactory.loading(true)
        commonFactory.ajax("/api/Statistics/QueryChannel", $scope.channellistviewData, function (result) {
            $scope.graybtn = false;
            commonFactory.loading(false)
            if (result.Code == 1) {
                result.Data = JSON.parse(result.Data)
                $scope.SourceTypes = result.Data.SourceTypes;
                $scope.channellist = result.Data.ChannelInfo;
                $('#myTable').html($scope.channellist);
                $timeout(function () {
                    $('.table')[0].id = "tableexcel";
                    $scope.channellistviewData.StartDate = commonFactory.DateFormat($scope.channellistviewData.StartDate, 1)
                    $scope.channellistviewData.EndDate = commonFactory.DateFormat($scope.channellistviewData.EndDate, 1)
                    $scope.excelname = '渠道统计'+$scope.channellistviewData.StartDate+'-'+$scope.channellistviewData.EndDate;
                    $scope.exportshow = true;
                    var checkbox = $("input[name=menucheckout]");
                    if (!$scope.channellistviewData.SourceTypeIds) {
                        for (var j = checkbox.length - 1; j >= 0; j--) {
                            checkbox[j].checked = "checked";
                        }
                    } else {
                        for (var i = 0; i < sourceTypesArr.length; i++) {
                            for (var j = checkbox.length - 1; j >= 0; j--) {
                                if (checkbox[j].value == sourceTypesArr[i]) {
                                    checkbox[j].checked = "checked";
                                }
                            }
                        }
                    }
                }, 10);

            } else {
                toaster.pop('error', '失败', result.Data)
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    getchannellist()
    $scope.querydata = function () {
        $scope.graybtn = true;
        sourceTypesArr = [];
        $scope.channellistviewData.SourceTypeIds = "";
        var checkbox = $("input[name=menucheckout]:checked");
        for (var i = 0; i < checkbox.length; i++) {
            sourceTypesArr.push(checkbox[i].value)
            if (i != checkbox.length - 1) {
                $scope.channellistviewData.SourceTypeIds += (checkbox[i].value + ',')
            } else {
                $scope.channellistviewData.SourceTypeIds += checkbox[i].value
            }
        }
        getchannellist()
    }
    $scope.selectAll = function () {
        sourceTypesArr = [];
        $scope.channellistviewData.SourceTypeIds = "";
        getchannellist()
    }

}]);
// 季度留存
app.controller('quarter', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$location', function ($scope, $http, commonFactory, toaster, $timeout, $location) {
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1;
    var sendDate = year + '-' + month;
    $scope.month = month
    $scope.da = {
        da: sendDate
    }
    $('#datetimepicker').datetimepicker({
        language: 'zh-CN',
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        minView: 3,
        forceParse: 0,
        todayBtn: "linked",
        keyboardNavigation: false,
        showMeridian: true,
        format: 'yyyy-mm',
        startView: 'year',
        pickerPosition: "bottom-right"
    }).on('changeMonth', function (ev) {
        var myear = ev.date.getFullYear()
        var mon = ev.date.getMonth() + 1
        $scope.month = mon
        var date =myear+'-'+mon
        $scope.da = {
            da: date
        }
    });

    $scope.initdate = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Statistics/GetQuarterStatistics", $scope.da, function (result) {
            $scope.graybtn = false;
            commonFactory.loading(false)
            if (result.Code == 1) {
                localStorage.getItem('ym', '');
                $scope.GetQuarterList = JSON.parse(result.Data);
            } else {
                toaster.pop('error', '失败', '返回错误')
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "get");
    }
    $scope.initdate()

}]);
//设备终端
app.controller('equipmentterminal', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$location', function ($scope, $http, commonFactory, toaster, $timeout, $location) {
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1;
    var sendDate = year + '-' + month;
    $scope.month = month
    $scope.da = {
        da: sendDate
    }
    $('#datetimepicker').datetimepicker({
        language: 'zh-CN',
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        minView: 3,
        forceParse: 0,
        todayBtn: "linked",
        keyboardNavigation: false,
        showMeridian: true,
        format: 'yyyy-mm',
        startView: 'year',
        pickerPosition: "bottom-right"
    }).on('changeMonth', function (ev) {
        var myear = ev.date.getFullYear()
        var mon = ev.date.getMonth() + 1;
        var date =myear+'-'+mon
        $scope.da = {
            da: date
        }
    });
    $scope.initdate = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Statistics/GetEquipmentTerminalStatistics", $scope.da, function (result) {
            commonFactory.loading(false)
            if (result.Code == 1) {
                localStorage.getItem('ym', '');
                $scope.GetQuarterList = JSON.parse(result.Data);
            } else {
                toaster.pop('error', '失败', '返回错误')
            }
        }, function () {
            commonFactory.loading(false)
        }, "get");
    }
    $scope.initdate()

}]);